$(function () {
  // 是否加载完成
  var loadBannerEnd = false
  var loadTypeEnd = false
  var loadListEnd = false
  $(".container-fluid").hide()
  // 设置横向滚动
  function setScroll() {
    var element = $(".main-screen .screen-box");
    element.on("wheel", function (event) {
      if (element.get(0).scrollWidth > element.innerWidth()) {
        event.preventDefault();
        var scrollAmount = event.originalEvent.deltaY;
        element.scrollLeft(element.scrollLeft() + scrollAmount);
      }
    });
  }
  // 设置分类筛选
  var levelId = 0
  $('#screen').on('click', '.item', function () {
    $(this).addClass("active").siblings().removeClass('active')
    levelId = $(this).attr("data-id")
    getEnterprise()
  })
  // 获取轮播图
  function getBanner() {
    $.ajax({
      method: 'GET',
      url: adminPath + '/api/wdsxh/banner/index',
      headers: {
        page: 4
      },
      success: function (res) {
        loadBannerEnd = true
        if (loadBannerEnd && loadTypeEnd && loadListEnd) {
          $(".container-fluid").show()
        }
        if (res.code == 1) {
          var indicatorsHtml = ""
          var innerHtml = ""
          for (var i in res.data) {
            indicatorsHtml += `<li data-target="#carouselExampleIndicators" data-slide-to="${i}" ${i == 0 ? 'class="active"' : ''}></li>`
            innerHtml += `
              <div class="carousel-item ${i == 0 ? 'active' : ''}">
                <img src="${res.data[i].pc_image}" class="w-100 normal">
                <img src="${res.data[i].image}" class="w-100 mobile">
              </div>
            `
          }
          $("#carouselExampleIndicators .carousel-indicators").html(indicatorsHtml)
          $("#carouselExampleIndicators .carousel-inner").html(innerHtml)
        } else {
          console.error(res.msg)
        }
      },
    })
  }
  // 获取会员级别
  function getMemberLevel() {
    $.ajax({
      method: 'GET',
      url: adminPath + '/api/wdsxh/member/level',
      success: function (res) {
        loadTypeEnd = true
        if (loadBannerEnd && loadTypeEnd && loadListEnd) {
          $(".container-fluid").show()
        }
        if (res.code == 1) {
          var screenHtml = ""
          for (var i in res.data) [
            screenHtml += `<div class="item" data-id="${res.data[i].id}">${res.data[i].name}</div>`
          ]
          $("#screen").append(screenHtml)
        } else {
          console.error(res.msg)
        }
      },
    })
  }
  // 获取会员单位列表
  var page = 1
  var limit = 9
  function getEnterprise() {
    $.ajax({
      method: 'GET',
      url: adminPath + '/api/wdsxh/member/company',
      data: {
        level: levelId,
        page: page,
        limit: limit,
      },
      success: function (res) {
        loadListEnd = true
        if (loadBannerEnd && loadTypeEnd && loadListEnd) {
          $(".container-fluid").show()
        }
        if (res.code == 1) {
          var listHtml = ""
          for (var i in res.data.list) {
            var tagHtml = ""
            for (var j in res.data.list[i].label) {
              tagHtml += `<div class="tag-item">${res.data.list[i].label[j]}</div>`
            }
            listHtml += `
              <a href="enterprise_detail.html?from=1&id=${res.data.list[i].id}" class="cont-item">
                <div class="image">
                  <img src="${res.data.list[i].company_logo}" alt="${res.data.list[i].company_name}">
                  <div class="post">${res.data.list[i].level.name}单位</div>
                </div>
                <div class="name normal">${res.data.list[i].company_name}</div>
                <div class="mobile">
                  <div class="name">${res.data.list[i].company_name}</div>
                  <div class="brief">${res.data.list[i].company_brief}</div>
                  <div class="tag flex">${tagHtml}</div>
                </div>
              </a>
            `
          }
          $(".container-main .column-cont").html(listHtml)
          if (listHtml) {
            $(".empty").hide()
          } else {
            $(".empty").css("display", "flex")
          }
          setPages(res.data.total)
        } else {
          console.error(res.msg)
        }
      },
    })
  }
  // 设置分页
  function setPages(total) {
    var nowPage = page
    var totalPage = Math.ceil(total / limit)
    if (nowPage == 1) {
      new Paging('page', {
        nowPage: nowPage, // 当前页码
        pageNum: totalPage, // 总页码
        totalNum: total, // 总条数
        buttonNum: 5, //要展示的页码数量
        canJump: 0,// 是否能跳转。0=不显示（默认），1=显示
        showOne: 0,//只有一页时，是否显示。0=不显示,1=显示（默认）
        callback: function (num) { //回调函数
          page = num
          getEnterprise()
        }
      })
    }
  }
  // 调用方法
  setScroll()
  getBanner()
  getMemberLevel()
  getEnterprise()
});